import os
import subprocess
import time
from datetime import datetime

from spider.download_m3u import m3u_to_grouped_txt, m3uPath, get_m3u


def auto_git_commit():
	"""全自动Git提交 - 使用当前时间作为提交信息"""
	# 1. 检查Git仓库
	if not os.path.exists('.git'):
		print("错误：当前目录不是Git仓库")
		return

	# 2. 添加所有更改
	subprocess.run(['git', 'add', '.'], stdout=subprocess.DEVNULL)

	# 3. 生成提交信息并提交
	commit_msg = f"更新时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
	subprocess.run(
		['git', 'commit', '-m', commit_msg],
		stdout=subprocess.DEVNULL,
		stderr=subprocess.PIPE
	)
	commit_result = subprocess.run(['git', 'push'], stdout=subprocess.DEVNULL)

	# 4. 处理结果
	if commit_result.returncode == 0:
		print(f"提交成功: {commit_msg}")
	else:
		# 检查是否没有需要提交的更改
		if "nothing to commit" in commit_result.stderr.decode():
			print("没有需要提交的更改")
		else:
			print(f"提交失败: {commit_result.stderr.decode()}")


if __name__ == '__main__':
	startTime = time.time()
	get_m3u()
	m3u_to_grouped_txt(m3uPath)
	endTime = time.time()
	print(f'程序运行总时间: {round((endTime - startTime) / 60, 2)} m')
	auto_git_commit()
